# This is the main file for data preparation and for the study
# "Majoritarian democracy udermines truth-finding in deliberative committees"
# Jan Lorenz, Heiko Rauhut, Bernhard Kittel
# written by Jan Lorenz
# It includes the computation and extraction of an extended groups table from the data.
# A subset of this groups table is saved as groups.dta to be used in the STATA regression 
# analysis in LorenzRauhutKittel_Regressions.do
# It includes code which produces the figures of paper and Online Appendix.
# You need latex (with pdflatex and tikz) and pdfcrop.

library(plotrix)
library(foreign)

# First you need to load data from the dataverse. Here it is loaded as a simple csv file
data=read.csv("data_v2.csv");

# CONSTANTS
institutionsText=c("Individual","Majority","Unanimity"); 
communicationText=c("Numbers","Chat");
QuestionAbbrev=c("Share of people in Asia",
  "Share of people in Americas",
  "Share of people in Africa", 
  "Water on earth surface",
  "Mass of iron in earth crust",
  "Mass of aluminium in earth crust",
  "Mass of silicium in earth crust",
  "Workers in agriculture",
  "Turnout EU parliament UK 1984",
  "Turnout EU parliament FR 1984",
  "Turnout EU parliament DK 1984",
  "Votes USA at World Bank",  
  "Water content tomato",
  "Water content onion",
  "Water content cucumber",
  "Use of own oil Saudi Arabia",
  "Turnout Bundestag DE 2009",
  "Turnout Bundestag DE 1998",
  "Turnout Bundestag DE 2005");
QuestionGroupedAbbrev=c(
  "Share of people in Asia/Americas/Africa", 
  "Share of people in Asia/Americas/Africa", 
  "Share of people in Asia/Americas/Africa", 
  "Water on earth surface",
  "Mass aluminium/silicium/iron in earth crust",
  "Mass aluminium/silicium/iron in earth crust",
  "Mass aluminium/silicium/iron in earth crust",
  "Workers in agriculture",
  "Turnout EU parliament DK/UK/FR 1984",
  "Turnout EU parliament DK/UK/FR 1984",
  "Turnout EU parliament DK/UK/FR 1984",
  "Votes USA at World Bank",  
  "Water content tomato/onion/cucumber",
  "Water content tomato/onion/cucumber",
  "Water content tomato/onion/cucumber",
  "Use of own oil Saudi Arabia",
  "Turnout Bundestag DE 1998/2005/2009",
  "Turnout Bundestag DE 1998/2005/2009",
  "Turnout Bundestag DE 1998/2005/2009"
);
QuestionGroupedID=c(rep("1a/b/c",3),2,rep("3a/b/c",3),4,rep("5a/b/c",3),6,rep("7a/b/c",3),8,rep("9a/b/c",3));

# Functions
payoff = function(dist) { # computes the payoff from the distance to the truth
  p= dist; p[!is.na(p)]=0;  p[dist<12] = 0.5;  p[dist<6] = 1;  p[dist<3] = 2;  p[dist<1.5] = 4;
  return(p)
}

# function which produces and compiles a figure of a run using tikz in latex
tikzrun = function(E,H,name,write.header=TRUE) {
  institutionsText=c("Individual","Majority","Consensus"); 
  if (H$Chat==0) {
    tmin=0;tmax=180;tscale=180;
    T = round(100*(tmax-E[,match(paste("T",1:10,sep=""),names(E))])/(tscale),digits=2);
    # Tmax = max(c(100,max(T$T10)));
    Tmax = max(T$T10);
    xGrid=(apply(T[,1:9],2,max)+apply(T[,2:10],2,min))/2;
  }
  # start tex-file
  filename = paste(name,".tex",sep="");
  write("\\documentclass{article}
        \\usepackage{eurosym}
        \\usepackage{tikz}
        \\usetikzlibrary{shapes.arrows}
        \\usetikzlibrary{matrix}
        \\begin{document}
        \\thispagestyle{empty}
        \\definecolor{corecol0}{rgb}{1,1,1}
        \\definecolor{corecol1}{rgb}{1,1,0.6667}
        \\definecolor{corecol2}{rgb}{1,1,0.1667}
        \\definecolor{corecol3}{rgb}{1,0.75,0}
        \\definecolor{corecol4}{rgb}{1,0.3750,0}
        \\definecolor{corecol5}{rgb}{1,0,0}
        \\definecolor{corecol6}{rgb}{0.6250,0,0}
        \\definecolor{textcol1}{rgb}{   0,   0,   0}
\\definecolor{textcol2}{rgb}{ 0.4, 0.4, 0.4}
\\definecolor{textcol3}{rgb}{0.25,0.25,0.25}
\\definecolor{textcol4}{rgb}{   0,   0,   0}
\\definecolor{textcol5}{rgb}{ 0.4, 0.4, 0.4}
\\definecolor{textcol6}{rgb}{0.25,0.25,0.25}
\\definecolor{textcol7}{rgb}{   0,   0,   0}
\\definecolor{textcol8}{rgb}{ 0.4, 0.4, 0.4}
\\definecolor{textcol9}{rgb}{0.25,0.25,0.25}
\\definecolor{textcol10}{rgb}{   0,   0,   0}
\\definecolor{textcol11}{rgb}{ 0.4, 0.4, 0.4}
\\definecolor{textcol12}{rgb}{0.25,0.25,0.25}
        \\begin{tikzpicture}[scale=0.02]
        \\sf \\footnotesize",file=filename);
  # Truth and Payment-Zones
  write(paste('\\fill[black!10] (-25,',min(H$Truth+12,100), ') rectangle (',Tmax+10,',',max(H$Truth-12,0), ');',sep=''),file=filename,append=TRUE);
  write(paste('\\fill[black!20] (-25,',min(H$Truth+6,100)  ,') rectangle (',Tmax+10,',',max(H$Truth-6,0),  ');',sep=''),file=filename,append=TRUE);
  write(paste('\\fill[black!30] (-25,',min(H$Truth+3,100),  ') rectangle (',Tmax+10,',',max(H$Truth-3,0),  ');',sep=''),file=filename,append=TRUE);
  write(paste('\\fill[black!40] (-25,',min(H$Truth+1.5,100),') rectangle (',Tmax+10,',',max(H$Truth-1.5,0),');',sep=''),file=filename,append=TRUE);
  write(paste('\\draw[black!60] (-25,',H$Truth,') -- (',Tmax+10,',',H$Truth,');',sep=''),file=filename,append=TRUE);
  # Grid
  if (H$Chat==0) {
    write(paste('\\draw[black!50,ultra thin] (',xGrid,',0) -- (',xGrid,',100);',sep=''),file=filename,append=TRUE);
  }
  else {
    write(paste('\\path (50,15) node[single arrow,draw,single arrow head extend=.5mm, black!30] {\\tiny chat};',sep=''),file=filename,append=TRUE);
    write(paste('\\path (50,85) node[single arrow,draw,single arrow head extend=.5mm, black!30] {\\tiny chat};',sep=''),file=filename,append=TRUE);
  }
  write(paste('\\draw[black!50,thick] (0,0) rectangle (',Tmax,',100) (',Tmax/2,',100);',sep=''),file=filename,append=TRUE);
  # x-Axis
  write(paste('\\draw[-] (-0,-5) -- (',Tmax,',-5);',sep=''),file=filename,append=TRUE);
  write(paste('\\draw (',Tmax/2,',-15) node[anchor=north,text width=',Tmax*0.6,',text centered] {\\tiny \\linespread{0.6}\\selectfont deliberation time (min) \\par };',sep=''),file=filename,append=TRUE);
  xTicks=seq(0,floor(Tmax*3/100),by=1)*100/3;
  write(paste('\\draw (',xTicks,',-5) +(0,0) -- +(0,-3) node[anchor=north,yshift=1.5] {\\tiny',xTicks*3/100,'};',sep=''),file=filename,append=TRUE);
  write(paste('\\draw (',Tmax,',-5) +(0,0) -- +(0,-3) ;',sep=''),file=filename,append=TRUE);
  write(paste('\\draw (-7.5,0) node[anchor=east,rotate=90,text centered] {\\tiny private} ;',sep=''),file=filename,append=TRUE);
  write(paste('\\draw (',Tmax+5,',0)  node[anchor=east,rotate=90,text centered] {\\tiny decision} ;',sep=''),file=filename,append=TRUE);
  # y-Axis
  write('\\draw[-] (-25,0) -- (-25,100) -- (-25,100) -- (-25,100);',file=filename,append=TRUE);
  write('\\draw (-48,50) node[rotate=90] {\\tiny Estimate (\\%)};',file=filename,append=TRUE);
  yTicks=c(0,25,50,75,100);
  write(paste('\\draw (-25,',yTicks,')  +(0,0) -- +(-3,0) node[anchor=east,xshift=1.5] {\\tiny ',yTicks,'};',sep=''),file=filename,append=TRUE);
  # Title and other text
  write(paste('\\draw (-50,115) node[anchor=west] {\\tiny {',H$QuestionAbbrev,' (',H$Truth,'\\%)}};',sep=''),file=filename,append=TRUE);
  # write(paste('\\draw (-25,115) node[anchor=west] {\\tiny Decision Rule: ',institutionsText[H$Institution+1],'};',sep=''),file=filename,append=TRUE);
  # functions
  trajectoryNum = function(IniEst,Est,T,Dec,Tmax,color) {
    text=paste("\\draw[",color,",thin] (-5,",IniEst,") -- (",T[1],',',Est[1],');',sep="");
    text[2]=paste("\\filldraw[draw=",color,",fill=white] (-5,",IniEst,") circle (2);",sep='');
    text[3] = paste("\\draw[",color,",thick] ",paste('(',T,',',Est,') circle (0.75) ',sep="",collapse=" -- "),';'  ,sep='');
    text[4]=paste("\\draw[",color,",thin] (",T[10],",",Est[10],") -- (",Tmax+5,',',Dec,');',sep="");
    text[5]=paste("\\filldraw[draw=",color,",fill=white] (",Tmax+5,',',Dec,") circle (2);)",sep='');
    return(text);
  }
  trajectoryChat = function(IniEst,Dec,Tmax,color) {
    text=paste("\\draw[",color,"] (-5,",IniEst,") circle (1.5);)",sep='');
    text[2] = paste("\\draw[",color,",thin] (-5,",IniEst,") -- (",Tmax+5,',',Dec,');',sep='');
    text[3]=paste("\\draw[",color,"] (",Tmax+5,',',Dec,") circle (1.5);)",sep='');
    return(text);
  }
  # Trajectories
  for (i in 1:nrow(E)) {
    if (H$Chat==0) {
      write(trajectoryNum(E[i,"InitialEstimate"],E[i,match(paste("Estimate",1:10,sep=""),names(E))],T[i,],E[i,"Decision"],Tmax,paste("textcol",i,sep="")),file=filename,append=TRUE);}
    else {
      write(trajectoryChat(E[i,"InitialEstimate"],E[i,"Decision"],Tmax,paste("textcol",i,sep="")),file=filename,append=TRUE);}
  }
  # Euro Payments
  write(paste('\\node[draw,fill=white,shape=rectangle,inner sep=1,rotate=90] at (-15,',median(E$InitialEstimate),') {\\tiny \\EUR{',E[E$InitialEstimate==median(E$InitialEstimate),"InitialPerformance"],'}};',sep=''),file=filename,append=TRUE);
  write(paste('\\node[draw,fill=white,shape=rectangle,inner sep=1,anchor=west] at (',Tmax+10,',',median(E$Decision),') {\\tiny \\EUR{',median(E[E$Decision==median(E$Decision),"Pay"]),'}};',sep=''),file=filename,append=TRUE);
  # Finish tikz tex-file
  write("\\end{tikzpicture}
        \\end{document}",file=filename,append=TRUE);
  system(paste("pdflatex",filename));
  system(paste("pdfcrop ",name,".pdf ",name,".pdf",sep=""));
#   system(paste("rm",filename));
  system(paste("rm ",name,".aux",sep=""));
  system(paste("rm ",name,".log",sep=""));
}

# function which produces and compiles a figure of a run using tikz 
tikzPerf = function(E,name) {
  Tmax=100;
  # start tex-file
  filename = paste(name,".tex",sep="");
  write("\\documentclass{article}
        \\usepackage{eurosym}
        \\usepackage{tikz}
        \\usetikzlibrary{shapes.arrows}
        \\begin{document}
        \\thispagestyle{empty}
        \\begin{tikzpicture}[scale=0.02]
        \\sf \\footnotesize",file=filename);
  write(paste('\\draw[-] (5,45) -- (95,45);',sep=''),file=filename,append=TRUE);
  write(paste('\\draw (',Tmax/2,',35) node[anchor=north,text width=',Tmax*0.6,',text centered] {\\tiny \\linespread{0.6}\\selectfont deliberation period \\par };',sep=''),file=filename,append=TRUE);
  xTicks=seq(5,95,by=10);
  xTickLabels=seq(1,10,by=1);
  write(paste('\\draw (',xTicks,',45) +(0,0) -- +(0,-3) node[anchor=north,yshift=1.5] {\\tiny',xTickLabels,'};',sep=''),file=filename,append=TRUE);
  write(paste('\\draw (-7.5,50) node[anchor=east,rotate=90,text centered] {\\tiny private} ;',sep=''),file=filename,append=TRUE);
  write(paste('\\draw (',Tmax+5,',50)  node[anchor=east,rotate=90,text centered] {\\tiny decision} ;',sep=''),file=filename,append=TRUE);
  # y-Axis
  write('\\draw[-] (-25,50) -- (-25,130);',file=filename,append=TRUE);
  write('\\draw (-58,100) node[rotate=90,text width=50,text centered] {\\tiny \\linespread{0.6} \\selectfont Average group performance (\\EUR{}) \\par};',file=filename,append=TRUE);
  yTicks=c(50,100,130);yTickLabels=c(0.5,1,1.3);
  write(paste('\\draw (-25,',yTicks,')  +(0,0) -- +(-3,0) node[anchor=east,xshift=1.5] {\\tiny ',yTickLabels,'};',sep=''),file=filename,append=TRUE);
  # Title and other text
  # write(paste('\\draw (-25,115) node[anchor=west] {\\tiny Decision Rule: ',institutionsText[H$Institution+1],'};',sep=''),file=filename,append=TRUE);
  # functions
  # Trajectories
  trajectoryNum = function(IniEst,Est,T,Dec,Tmax,color) {
    text=paste("\\draw[",color,",thin] (-5,",IniEst,") -- (",T[1],',',Est[1],');',sep="");
    text[2]=paste("\\filldraw[draw=",color,",fill=white] (-5,",IniEst,") circle (2);",sep='');
    text[3] = paste("\\draw[",color,",thick] ",paste('(',T,',',Est,') circle (0.75) ',sep="",collapse=" -- "),';'  ,sep='');
    text[4]=paste("\\draw[",color,",thin] (",T[10],",",Est[10],") -- (",Tmax+5,',',Dec,');',sep="");
    text[5]=paste("\\filldraw[draw=",color,",fill=white] (",Tmax+5,',',Dec,") circle (2);)",sep='');
    return(text);
  }
  write(trajectoryNum(E[1],E[2:11],seq(5,95,by=10),E[12],Tmax,"black"),file=filename,append=TRUE);
  # Euro Payments
  write(paste('\\node[draw,fill=white,shape=rectangle,inner sep=1,rotate=90] at (-15,',E[1],') {\\tiny \\EUR{',round(E[1]/100,digits=2),'}};',sep=''),file=filename,append=TRUE);
  write(paste('\\node[draw,fill=white,shape=rectangle,inner sep=1,anchor=west] at (110,',E[12],') {\\tiny \\EUR{',round(E[12]/100,digits=2),'}};',sep=''),file=filename,append=TRUE);
  # Finish tikz tex-file
  write("\\end{tikzpicture}
\\end{document}",file=filename,append=TRUE);
  system(paste("pdflatex",filename));
  system(paste("pdfcrop ",name,".pdf ",name,".pdf",sep=""));
  system(paste("rm",filename));
  system(paste("rm ",name,".aux",sep=""));
  system(paste("rm ",name,".log",sep=""));
}

# Compute r (groups dataset) from data.csv and save as groups.csv and groups.dta for stata regression
data$InitialPerformance=payoff(abs(data$InitialEstimate-data$Truth));
r=merge(aggregate(data[,c("numInGroup","Chat","Institution","Truth","InitialEstimate",paste("Estimate",1:10,sep=""),
                          "Decision","GroupDecision")],list(runID=data$runID),median),
        aggregate(data[,c("Pay","Certainty1","Certainty2")],list(runID=data$runID),mean))
r$QuestionID=data[data$Member==1,"QuestionID"];
r$QuestionAbbrev=r$QuestionID;levels(r$QuestionAbbrev)=QuestionAbbrev;
r$QuestionGroupedAbbrev=r$QuestionID;levels(r$QuestionGroupedAbbrev)=QuestionGroupedAbbrev;
r$QuestionGroupedID=r$QuestionID;levels(r$QuestionGroupedID)=QuestionGroupedID;
# Performance
r$InitialPerformance=payoff(abs(r$InitialEstimate-r$Truth));
for (i in 1:10) {r[,paste("IntermediatePerformance",i,sep="")]=payoff(abs(r[,paste("Estimate",i,sep="")]-r$Truth)); }
r$FinalPerformance=r$Pay; # for majority and consensus, for individual next line exchanges it
r[r$Institution==0,"FinalPerformance"]=payoff(abs(r[r$Institution==0,"Decision"]-r[r$Institution==0,"Truth"]));
# Distance
r$InitialDistance=abs(r$InitialEstimate-r$Truth);
for (i in 1:10) {r[,paste("IntermediateDistance",i,sep="")]=abs(r[,paste("Estimate",i,sep="")]-r$Truth); }
r[,"FinalDistance"]=abs(r[,"Decision"]-r[,"Truth"]);
# Change
r$ChangePerformance=r$FinalPerformance-r$InitialPerformance;
r$ChangeDistance=r$FinalDistance-r$InitialDistance;
r$ChangeCertainty=r$Certainty2-r$Certainty1;

write.csv(r,file="groups.csv",row.names=FALSE);
write.csv(data,file="data.csv",row.names=FALSE);
groups=r[,c("runID","numInGroup","Chat","Institution","QuestionID",
            "InitialPerformance","FinalPerformance","ChangePerformance",
            "InitialDistance","FinalDistance","ChangeDistance")];
write.dta(groups,"groups.dta");
write.dta(data,"data.dta");

          
# FIGURES

# Fig. 1 (and Fig. 5 of the Online Appendix): Histograms
for (q in 1:length(levels(data[["QuestionID"]]))) {
  for (i in c(0:2,3)) { # Institutions, 3 for Initial Estimates
    qID=levels(data[["QuestionID"]])[q];
    if (i<3) {Instinds = data$Institution==i;est = "Decision"}
    else {Instinds = data$Institution<=2;est = "InitialEstimate";} 
    if (!all(!(data$QuestionID==qID & Instinds))) {
      pdf(file=paste('histQ',qID,'I',i,'.pdf',sep=''),height=2.7,width=4);
      titlestr=paste('Q',qID,': ',switch(i+1,
                                         'Decision Individual',
                                         'Decision Majority',
                                         'Decision Unanimity',
                                         paste('Initial Estimate \n',QuestionAbbrev[q],sep="")),
                     sep="")
      truth=data[data$QuestionID==qID,"Truth"][1];
      if (i == 3) {h=hist(data[data$QuestionID==qID & Instinds,est],breaks=seq(0,100,5),plot=FALSE);maxy=max(h$counts);}
      else {hC=hist(data[data$QuestionID==qID & Instinds & data$Chat==1,est],breaks=seq(0,100,5),plot=FALSE);
            hN=hist(data[data$QuestionID==qID & Instinds & data$Chat==0,est],breaks=seq(0,100,5),plot=FALSE);
            maxy=max(hC$counts + hN$counts)}
      plot.new();
      plot.window(xlim=c(0,100),ylim=c(0,1.1*maxy));
      rect(max(truth-12,0),-0.1*maxy,min(truth+12,100),1.1*maxy,border=NA,col=rgb(.85,.85,.85));
      rect(max(truth-6,0),-0.1*maxy,min(truth+6,100),1.1*maxy,border=NA,col=rgb(.7,.7,.7));
      rect(max(truth-3,0),-0.1*maxy,min(truth+3,100),1.1*maxy,border=NA,col=rgb(.5,.5,.5));
      rect(max(truth-1.5,0),-0.1*maxy,min(truth+1.5,100),1.1*maxy,border=NA,col=rgb(.3,.3,.3));
      lines(c(truth,truth),c(-0.1*maxy,1.1*maxy),type='l',lwd=3);
      axis(1);axis(2);title(main=titlestr,adj=0)
      if (i == 3) { barplot(h$counts,col=rgb(0,.5,0),
                            width=5,space=0,add=TRUE); }
      else { barplot(rbind(hC$counts,hN$counts),col=c(switch(i+1,rgb(0,0,1),rgb(1,0,0),rgb(0.5,0,0.5)),
                                                      switch(i+1,rgb(0.5,0.5,1),rgb(1,0.5,0.5),rgb(0.75,0.25,0.75))),
                     width=5,space=0,add=TRUE); }
      dev.off();
    }
  }
}


# Fig. 2A
D = aggregate(data.frame(perfimprov=r[,"ChangePerformance"]),list(Inst=r$Institution),"mean");
D_1var = aggregate(data.frame(perfimprov=r[,"ChangePerformance"]),list(Inst=r$Institution),"sd");
clev=0.856; 
for (i in 1:3) {
  D[i,"li"] = t.test(r[r$Institution==D[i,"Inst"],"ChangePerformance"],conf.level=clev)$conf.int[1];
  D[i,"ui"] = t.test(r[r$Institution==D[i,"Inst"],"ChangePerformance"],conf.level=clev)$conf.int[2];
}
pdf(file="fig2A.pdf",width=3.5,height=3.5);
par("mar"=c(3,5,2,1)+.1);
plotCI(D$Inst,D$perfimprov,li=D$li,ui=D$ui,col="black",lwd=1,pch=16,xlim=c(-0.2,2.2),
       xlab="",xaxt="n",yaxp=c(-0.6,0.6,4),ylab="change in group performance (EUR)",las=1);
lines(D$Inst,D$perfimprov);
axis(1,at=c(0,1,2),labels=institutionsText,las=1);
dev.off();
# Fig. 2B
D = aggregate(data.frame(perfimprov=r[,"ChangeCertainty"]),list(Inst=r$Institution),"mean");
D_2var = aggregate(data.frame(perfimprov=r[,"ChangeCertainty"]),list(Inst=r$Institution),"var");
clev=0.856; 
for (i in 1:3) {
  D[i,"li"] = t.test(r[r$Institution==D[i,"Inst"],"ChangeCertainty"],conf.level=clev)$conf.int[1];
  D[i,"ui"] = t.test(r[r$Institution==D[i,"Inst"],"ChangeCertainty"],conf.level=clev)$conf.int[2];
}
pdf(file="fig2B.pdf",width=3.5,height=3.5);
par("mar"=c(3,5,2,1)+.1);
plotCI(D$Inst,D$perfimprov,li=D$li,ui=D$ui,col="black",lwd=1,pch=16,xlim=c(-0.2,2.2),
       xlab="",xaxt="n",ylab="change in group certainty (1-6)",las=1);
lines(D$Inst,D$perfimprov);
axis(1,at=c(0,1,2),labels=institutionsText,las=1);
dev.off();

# Fig. 3
Dall = aggregate(data.frame(perfimprov=r[,"ChangePerformance"]),list(Inst=r$Institution),"mean");
clev=0.856;
for (i in 1:3) {
  Dall[i,"li"] = t.test(r[r$Institution==Dall[i,"Inst"],"ChangePerformance"],conf.level=clev)$conf.int[1];
  Dall[i,"ui"] = t.test(r[r$Institution==Dall[i,"Inst"],"ChangePerformance"],conf.level=clev)$conf.int[2];
}
# Fig. 3A
D = aggregate(data.frame(perfimprov=r[,"ChangePerformance"]),list(Inst=r$Institution,Groupsize=r$numInGroup),"mean");
clev=0.856; 
for (i in 1:nrow(D)) {
  D[i,"li"] = t.test(r[r$Institution==D[i,"Inst"]&r$numInGroup==D[i,"Groupsize"],"ChangePerformance"],conf.level=clev)$conf.int[1];
  D[i,"ui"] = t.test(r[r$Institution==D[i,"Inst"]&r$numInGroup==D[i,"Groupsize"],"ChangePerformance"],conf.level=clev)$conf.int[2];
}
pdf(file="fig3A.pdf",width=3.5,height=3.5);
par("mar"=c(3,5,2,1)+.1);
plotCI(Dall[,"Inst"],Dall[,"perfimprov"],li=Dall[,"li"],ui=Dall[,"ui"],col="gray",lwd=1,pch=16,xlim=c(-0.2,2.2),
       xlab="",xaxt="n",ylim=c(-1.4,1.1),yaxp=c(-0.8,0.8,4),ylab="change in group performance (EUR)",las=1);
lines(Dall[,"Inst"],Dall[,"perfimprov"],col="gray");
plotCI(D[D$Groupsize==3,"Inst"]-0.1,D[D$Groupsize==3,"perfimprov"],li=D[D$Groupsize==3,"li"],ui=D[D$Groupsize==3,"ui"],col="black",lwd=1,lty=2,pch=16,xlim=c(-0.2,2.2),
       xlab="",xaxt="n",ylab="change in group performance (EUR)",las=1,add=TRUE);
lines(D[D$Groupsize==3,"Inst"]-0.1,D[D$Groupsize==3,"perfimprov"],col="black",lwd=2,lty=2);
plotCI(D[D$Groupsize==9,"Inst"]+0.1,D[D$Groupsize==9,"perfimprov"],li=D[D$Groupsize==9,"li"],ui=D[D$Groupsize==9,"ui"],col="black",lwd=1,lty=2,pch=16,xlim=c(-0.2,2.2),
       xlab="",xaxt="n",ylab="change in group performance (EUR)",las=1,add=TRUE);
lines(D[D$Groupsize==9,"Inst"]+0.1,D[D$Groupsize==9,"perfimprov"],col="black",lwd=2,lty=1);
axis(1,at=c(0,1,2),labels=institutionsText,las=1);
legend("bottom",legend=c("all groups","small groups (3 individuals)","large groups (9 individuals)"),col=c("gray","black","black"),lty=c(1,2,1),lwd=c(1,2,2),bty="n",cex=0.8);
dev.off();
# Fig. 3B
D = aggregate(data.frame(perfimprov=r[,"ChangePerformance"]),list(Inst=r$Institution,Chat=r$Chat),"mean");
clev=0.856; 
for (i in 1:nrow(D)) {
  D[i,"li"] = t.test(r[r$Institution==D[i,"Inst"]&r$Chat==D[i,"Chat"],"ChangePerformance"],conf.level=clev)$conf.int[1];
  D[i,"ui"] = t.test(r[r$Institution==D[i,"Inst"]&r$Chat==D[i,"Chat"],"ChangePerformance"],conf.level=clev)$conf.int[2];
}
pdf(file="fig3B.pdf",width=3.5,height=3.5);
par("mar"=c(3,5,2,1)+.1);
plotCI(Dall[,"Inst"],Dall[,"perfimprov"],li=Dall[,"li"],ui=Dall[,"ui"],col="gray",lwd=1,pch=16,xlim=c(-0.2,2.2),
       xlab="",xaxt="n",ylim=c(-1.4,1.1),yaxp=c(-0.8,0.8,4),ylab="change in group performance (EUR)",las=1);
lines(Dall[,"Inst"],Dall[,"perfimprov"],col="gray");
plotCI(D[D$Chat==0,"Inst"]-0.1,D[D$Chat==0,"perfimprov"],li=D[D$Chat==0,"li"],ui=D[D$Chat==0,"ui"],col="black",lwd=1,pch=16,xlim=c(-0.2,2.2),
       xlab="",xaxt="n",ylab="change in group performance (EUR)",las=1,add=TRUE);
lines(D[D$Chat==0,"Inst"]-0.1,D[D$Chat==0,"perfimprov"],col="black",lwd=2,lty=1);
plotCI(D[D$Chat==1,"Inst"]+0.1,D[D$Chat==1,"perfimprov"],li=D[D$Chat==1,"li"],ui=D[D$Chat==1,"ui"],col="black",lwd=1,pch=16,xlim=c(-0.2,2.2),
       xlab="",xaxt="n",ylab="change in group performance (EUR)",las=1,add=TRUE);
lines(D[D$Chat==1,"Inst"]+0.1,D[D$Chat==1,"perfimprov"],col="black",lwd=2,lty=2);
axis(1,at=c(0,1,2),labels=institutionsText,las=1);
legend("bottom",legend=c("all groups","chat communication","communication by numbers"),col=c("gray","black","black"),lty=c(1,2,1),lwd=c(1,2,2),bty="n",cex=0.8);
dev.off();


# Fig. 4
# nums = c(164,23,82,41,70); # run 20 extracted as tex File and modified manually for annotations
nums = c(163,23,81,41,70); # run 20 extracted as tex File and modified manually for annotations
for (i in 1:length(nums)) {
  E=data[data$runID==nums[i],]; H=r[r$runID==nums[i],];
  name=paste('fig4_run',nums[i],sep='');
  tikzrun(E,H,name);
}
E=100*apply(r[r$Chat==0 & r$Institution==0,c("InitialPerformance",paste("IntermediatePerformance",1:10,sep=""),"FinalPerformance")],2,mean);
tikzPerf(E,"fig4_PerfI");
E=100*apply(r[r$Chat==0 & r$Institution==1,c("InitialPerformance",paste("IntermediatePerformance",1:10,sep=""),"FinalPerformance")],2,mean);
tikzPerf(E,"fig4_PerfM");
E=100*apply(r[r$Chat==0 & r$Institution==2,c("InitialPerformance",paste("IntermediatePerformance",1:10,sep=""),"FinalPerformance")],2,mean);
tikzPerf(E,"fig4_PerfC");


# t-Tests - Examples
t.test(r[r$Institution==0,"ChangePerformance"])
t.test(r[r$Institution==0,"ChangePerformance"],r[r$Institution==1,"ChangePerformance"])
inds=r$Chat==0;
t=t.test(r[r$Institution==0&inds,"ChangePerformance"],r[r$Institution==1&inds,"ChangePerformance"]);list(t$estimate[1]-t$estimate[2],t$p.value)

t.test(r[r$Institution==0,"ChangeDistance"])
t.test(r[r$Institution==1,"ChangeDistance"],r[r$Institution==2,"ChangeDistance"])
inds=r$numInGroup==9;
t.test(r[r$Institution==0&inds,"ChangeDistance"],r[r$Institution==1&inds,"ChangeDistance"]);

# Online Appendix Fig. 1 decomposition by question
D = cbind(aggregate(data.frame(Ind=r[r$Institution==0,"ChangePerformance"]),list(Q=r[r$Institution==0,"QuestionGroupedID"],Qt=r[r$Institution==0,"QuestionGroupedAbbrev"]),"mean"),
          Maj=aggregate(data.frame(Maj=r[r$Institution==1,"ChangePerformance"]),list(Q=r[r$Institution==1,"QuestionGroupedID"]),"mean")$Maj,
          Con=aggregate(data.frame(Con=r[r$Institution==2,"ChangePerformance"]),list(Q=r[r$Institution==2,"QuestionGroupedID"]),"mean")$Con);
Dall = aggregate(data.frame(perfimprov=r[,"ChangePerformance"]),list(Inst=r$Institution),"mean");
pdf(file="decomp_questions.pdf",width=7,height=4);
par("mar"=c(3,5,2,1)+.1);
cols=c("red","darkgreen","blue","brown","darkviolet");
xtick=c(0,1.2,2.4);
plot(xtick,Dall[,"perfimprov"],type="o",col="gray",lwd=2,pch=16,xlim=c(-0.2,7),ylim=c(-1.7,2.1),xaxt="n",ylab="change in group performance (EUR)",las=1);
q=c(2,4,6,8);
for (l in 1:length(q)) {lines(xtick,D[D$Q==q[l],c("Ind","Maj","Con")],type="o",col=cols[l],lwd=1,pch=16);}
q=c("1a/b/c","3a/b/c","5a/b/c","7a/b/c","9a/b/c");
for (l in 1:length(q)) {lines(xtick,D[D$Q==q[l],c("Ind","Maj","Con")],type="o",lty="dashed",col=cols[l],lwd=1,pch=16)}
axis(1,at=xtick,labels=institutionsText,las=1);
legend("right",legend=c(paste("Q",1:9," ",D$Qt,sep="")),col=c(rep(cols[1:4],each=2),cols[5],"gray","white"),pch=16,lwd=c(rep(1,9),2),lty=c(rep(c("dashed","solid"),5)),bty="n",cex=0.8);
dev.off();

# Online Appendix Fig. 2 alternative measure final performance
D = aggregate(data.frame(perfimprov=r[,"FinalPerformance"]),list(Inst=r$Institution),"mean");
D_1var = aggregate(data.frame(perfimprov=r[,"FinalPerformance"]),list(Inst=r$Institution),"sd");
clev=0.856; 
for (i in 1:3) {
  D[i,"li"] = t.test(r[r$Institution==D[i,"Inst"],"FinalPerformance"],conf.level=clev)$conf.int[1];
  D[i,"ui"] = t.test(r[r$Institution==D[i,"Inst"],"FinalPerformance"],conf.level=clev)$conf.int[2];
}
pdf(file="fig2AFinal.pdf",width=3.5,height=3.5);
par("mar"=c(3,5,2,1)+.1);
plotCI(D$Inst,D$perfimprov,li=D$li,ui=D$ui,col="black",lwd=1,pch=16,xlim=c(-0.2,2.2),
       xlab="",xaxt="n",ylim=c(0,2),ylab="group performance (EUR)",las=1);
lines(D$Inst,D$perfimprov);
axis(1,at=c(0,1,2),labels=institutionsText,las=1);
dev.off();

# Online Appendix Fig. 2  alternative measure final performance: decomposition by question
D = cbind(aggregate(data.frame(Ind=r[r$Institution==0,"FinalPerformance"]),list(Q=r[r$Institution==0,"QuestionGroupedID"],Qt=r[r$Institution==0,"QuestionGroupedAbbrev"]),"mean"),
          Maj=aggregate(data.frame(Maj=r[r$Institution==1,"FinalPerformance"]),list(Q=r[r$Institution==1,"QuestionGroupedID"]),"mean")$Maj,
          Con=aggregate(data.frame(Con=r[r$Institution==2,"FinalPerformance"]),list(Q=r[r$Institution==2,"QuestionGroupedID"]),"mean")$Con);
Dall = aggregate(data.frame(perfimprov=r[,"FinalPerformance"]),list(Inst=r$Institution),"mean");
pdf(file="decomp_questionsFinal.pdf",width=7,height=4);
par("mar"=c(3,5,2,1)+.1);
cols=c("red","darkgreen","blue","brown","darkviolet");
xtick=c(0,1.2,2.4);
plot(xtick,Dall[,"perfimprov"],type="o",col="gray",lwd=2,pch=16,xlim=c(-0.2,7),ylim=c(0,4),xaxt="n",ylab="group performance (EUR)",las=1);
q=c(2,4,6,8);
for (l in 1:length(q)) {lines(xtick,D[D$Q==q[l],c("Ind","Maj","Con")],type="o",col=cols[l],lwd=1,pch=16);}
q=c("1a/b/c","3a/b/c","5a/b/c","7a/b/c","9a/b/c");
for (l in 1:length(q)) {lines(xtick,D[D$Q==q[l],c("Ind","Maj","Con")],type="o",lty="dashed",col=cols[l],lwd=1,pch=16)}
axis(1,at=xtick,labels=institutionsText,las=1);
legend("right",legend=c(paste("Q",1:9," ",D$Qt,sep="")),col=c(rep(cols[1:4],each=2),cols[5],"gray","white"),pch=16,lwd=c(rep(1,9),2),lty=c(rep(c("dashed","solid"),5)),bty="n",cex=0.8);
dev.off();

# Online Appendix Fig. 3 alternative measure change in distance to truth
D = aggregate(data.frame(perfimprov=r[,"ChangeDistance"]),list(Inst=r$Institution),"mean",na.rm=TRUE);
D_1var = aggregate(data.frame(perfimprov=r[,"ChangeDistance"]),list(Inst=r$Institution),"sd",na.rm=TRUE);
clev=0.856; 
for (i in 1:3) {
  D[i,"li"] = t.test(r[r$Institution==D[i,"Inst"],"ChangeDistance"],conf.level=clev)$conf.int[1];
  D[i,"ui"] = t.test(r[r$Institution==D[i,"Inst"],"ChangeDistance"],conf.level=clev)$conf.int[2];
}
pdf(file="fig2ADistance.pdf",width=3.5,height=3.5);
par("mar"=c(3,5,2,1)+.1);
plotCI(D$Inst,D$perfimprov,li=D$li,ui=D$ui,col="black",lwd=1,pch=16,xlim=c(-0.2,2.2),
       xlab="",xaxt="n",ylab="change in distance to truth",las=1);
lines(D$Inst,D$perfimprov);
axis(1,at=c(0,1,2),labels=institutionsText,las=1);
dev.off();

# Online Appendix Fig. 3 alternative measure change in distance to truth: decomposition by question
D = cbind(aggregate(data.frame(Ind=r[r$Institution==0,"ChangeDistance"]),list(Q=r[r$Institution==0,"QuestionGroupedID"],Qt=r[r$Institution==0,"QuestionGroupedAbbrev"]),"mean",na.rm=TRUE),
          Maj=aggregate(data.frame(Maj=r[r$Institution==1,"ChangeDistance"]),list(Q=r[r$Institution==1,"QuestionGroupedID"]),"mean",na.rm=TRUE)$Maj,
          Con=aggregate(data.frame(Con=r[r$Institution==2,"ChangeDistance"]),list(Q=r[r$Institution==2,"QuestionGroupedID"]),"mean",na.rm=TRUE)$Con);
Dall = aggregate(data.frame(perfimprov=r[,"ChangeDistance"]),list(Inst=r$Institution),"mean",na.rm=TRUE);
pdf(file="decomp_questionsDistance.pdf",width=7,height=4);
par("mar"=c(3,5,2,1)+.1);
cols=c("red","darkgreen","blue","brown","darkviolet");
xtick=c(0,1.2,2.4);
plot(xtick,Dall[,"perfimprov"],type="o",col="gray",lwd=2,pch=16,xlim=c(-0.2,7),ylim=c(-25,15),xaxt="n",ylab="change in distance to truth",las=1);
q=c(2,4,6,8);
for (l in 1:length(q)) {lines(xtick,D[D$Q==q[l],c("Ind","Maj","Con")],type="o",col=cols[l],lwd=1,pch=16);}
q=c("1a/b/c","3a/b/c","5a/b/c","7a/b/c","9a/b/c");
for (l in 1:length(q)) {lines(xtick,D[D$Q==q[l],c("Ind","Maj","Con")],type="o",lty="dashed",col=cols[l],lwd=1,pch=16)}
axis(1,at=xtick,labels=institutionsText,las=1);
legend("right",legend=c(paste("Q",1:9," ",D$Qt,sep="")),col=c(rep(cols[1:4],each=2),cols[5],"gray","white"),pch=16,lwd=c(rep(1,9),2),lty=c(rep(c("dashed","solid"),5)),bty="n",cex=0.8);
dev.off();

# Online Appendix Fig. 4 alternative measure in distance to truth
D = aggregate(data.frame(perfimprov=r[,"FinalDistance"]),list(Inst=r$Institution),"mean",na.rm=TRUE);
D_1var = aggregate(data.frame(perfimprov=r[,"FinalDistance"]),list(Inst=r$Institution),"sd",na.rm=TRUE);
clev=0.856; 
for (i in 1:3) {
  D[i,"li"] = t.test(r[r$Institution==D[i,"Inst"],"FinalDistance"],conf.level=clev)$conf.int[1];
  D[i,"ui"] = t.test(r[r$Institution==D[i,"Inst"],"FinalDistance"],conf.level=clev)$conf.int[2];
}
pdf(file="fig2AFinalDistance.pdf",width=3.5,height=3.5);
par("mar"=c(3,5,2,1)+.1);
plotCI(D$Inst,D$perfimprov,li=D$li,ui=D$ui,col="black",lwd=1,pch=16,xlim=c(-0.2,2.2),
       xlab="",xaxt="n",ylab="distance to truth",las=1);
lines(D$Inst,D$perfimprov);
axis(1,at=c(0,1,2),labels=institutionsText,las=1);
dev.off();

# Online Appendix Fig. 4 alternative measure distance to truth: decomposition by question
D = cbind(aggregate(data.frame(Ind=r[r$Institution==0,"FinalDistance"]),list(Q=r[r$Institution==0,"QuestionGroupedID"],Qt=r[r$Institution==0,"QuestionGroupedAbbrev"]),"mean",na.rm=TRUE),
          Maj=aggregate(data.frame(Maj=r[r$Institution==1,"FinalDistance"]),list(Q=r[r$Institution==1,"QuestionGroupedID"]),"mean",na.rm=TRUE)$Maj,
          Con=aggregate(data.frame(Con=r[r$Institution==2,"FinalDistance"]),list(Q=r[r$Institution==2,"QuestionGroupedID"]),"mean",na.rm=TRUE)$Con);
Dall = aggregate(data.frame(perfimprov=r[,"FinalDistance"]),list(Inst=r$Institution),"mean",na.rm=TRUE);
pdf(file="decomp_questionsFinalDistance.pdf",width=7,height=4);
par("mar"=c(3,5,2,1)+.1);
cols=c("red","darkgreen","blue","brown","darkviolet");
xtick=c(0,1.2,2.4);
plot(xtick,Dall[,"perfimprov"],type="o",col="gray",lwd=2,pch=16,xlim=c(-0.2,7),ylim=c(0,30),xaxt="n",ylab="distance to truth",las=1);
q=c(2,4,6,8);
for (l in 1:length(q)) {lines(xtick,D[D$Q==q[l],c("Ind","Maj","Con")],type="o",col=cols[l],lwd=1,pch=16);}
q=c("1a/b/c","3a/b/c","5a/b/c","7a/b/c","9a/b/c");
for (l in 1:length(q)) {lines(xtick,D[D$Q==q[l],c("Ind","Maj","Con")],type="o",lty="dashed",col=cols[l],lwd=1,pch=16)}
axis(1,at=xtick,labels=institutionsText,las=1);
legend("right",legend=c(paste("Q",1:9," ",D$Qt,sep="")),col=c(rep(cols[1:4],each=2),cols[5],"gray","white"),pch=16,lwd=c(rep(1,9),2),lty=c(rep(c("dashed","solid"),5)),bty="n",cex=0.8);
dev.off();

